package com.vsen.lpr.service;

import com.vsen.lpr.common.exception.BusinessException;
import com.vsen.lpr.common.model.Response;
import com.vsen.lpr.common.utils.BeanCopyUtil;
import com.vsen.lpr.common.utils.ThreadPoolUtil;
import com.vsen.lpr.entity.LabelType;
import com.vsen.lpr.entity.RelationShip;
import com.vsen.lpr.entity.TimeList;
import com.vsen.lpr.entity.TimeListLabel;
import com.vsen.lpr.entity.ext.TimeListDo;
import com.vsen.lpr.mapper.LabelTypeMapper;
import com.vsen.lpr.mapper.RelationShipMapper;
import com.vsen.lpr.mapper.TimeListLabelMapper;
import com.vsen.lpr.mapper.TimeListMapper;
import com.vsen.lpr.pojo.dto.InsertTimeListDto;
import com.vsen.lpr.pojo.dto.queryTimeListDto;
import com.vsen.lpr.pojo.vo.*;
import org.checkerframework.checker.units.qual.A;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.concurrent.*;

@Service
public class TimeListService {

    @Autowired
    private TimeListMapper timeListMapper;

    @Autowired
    private TimeListLabelMapper timeListLabelMapper;

    @Autowired
    private RelationShipMapper relationShipMapper;

    public Response insert(InsertTimeListDto dto) {
        if(StringUtils.isEmpty(dto.getText())){
            throw new BusinessException("60001","请输入内容");
        }
        TimeList timeList = new TimeList();
        BeanCopyUtil.copyFromSrcToDest(dto, timeList);
        timeListMapper.insert(timeList);
        return Response.success();
    }

    public Response<List<MyTimeListVo>> timeList(queryTimeListDto dto) {
        Integer type = dto.getType();
        String text = dto.getText();
        String labelName = dto.getLabelName();
        String userId = dto.getUserId();
        List<AggrTimeListVo> resultList = new ArrayList<>();
        List<String> years = timeListMapper.selectYear(userId);
        if (!CollectionUtils.isEmpty(years)) {
            for (String year : years) {
                AggrTimeListVo aggrTimeListVo = new AggrTimeListVo();
                //每年的所有数据
                List<TimeListDo> timeListDos = timeListMapper.selectByYear(year, userId, labelName, text, type);

                List<TimeListDayVo> dayVos = new ArrayList<>();

                //格式化为每天的map
                LinkedHashMap<String,List<TimeListDo>> dayMap = new LinkedHashMap<>();
                for(TimeListDo timeListDo : timeListDos){
                    if(dayMap.containsKey(timeListDo.getDateTime())){
                        List<TimeListDo> dayList = dayMap.get(timeListDo.getDateTime());
                        dayList.add(timeListDo);
                    }else{
                        List<TimeListDo> dayList = new ArrayList<>();
                        dayList.add(timeListDo);
                        dayMap.put(timeListDo.getDateTime(),dayList);
                    }
                }

                for (String key : dayMap.keySet()) {
                    TimeListDayVo timeListDayVo = new TimeListDayVo();
                    List<TimeListVo> list = BeanCopyUtil.copyListFromSrcToDest(dayMap.get(key), TimeListVo.class);
                    timeListDayVo.setList(list);
                    timeListDayVo.setDate(key);
                    dayVos.add(timeListDayVo);
                }
                aggrTimeListVo.setDays(dayVos);
                aggrTimeListVo.setYear(year);
                resultList.add(aggrTimeListVo);
            }
        }
        List<MyTimeListVo> myResult = new ArrayList<>();
        for(AggrTimeListVo aggrTimeListVo : resultList){
            MyTimeListVo myTimeListVo = new MyTimeListVo();
            myTimeListVo.setYear(aggrTimeListVo.getYear());
            myTimeListVo.setTimeType(1);//最外层 type类型为1
            myResult.add(myTimeListVo);
            List<TimeListDayVo> days = aggrTimeListVo.getDays();
            for(TimeListDayVo timeListDayVo : days){
                MyTimeListVo myTimeDayVo = new MyTimeListVo();
                myTimeDayVo.setDateTime(timeListDayVo.getDate().substring(5));
                myTimeDayVo.setTimeType(2);//日期 为2
                myResult.add(myTimeDayVo);
                List<TimeListVo> list = timeListDayVo.getList();
                for(TimeListVo timeListVo : list){
                    MyTimeListVo dayVo = new MyTimeListVo();
                    dayVo.setText(timeListVo.getText());
                    dayVo.setId(timeListVo.getId());
                    dayVo.setLabelName(timeListVo.getLabelName());
                    dayVo.setTimeType(3);//真正的文本
                    dayVo.setUserId(timeListVo.getUserId());
                    myResult.add(dayVo);
                }
            }
        }

        return Response.success(myResult);
    }


    public Response<List<LabelTypeVo>> labelList(queryTimeListDto dto) {
        String userId = dto.getUserId();
        String loveId = relationShipMapper.selectLoveId(userId);
        List<TimeListLabel> list =  timeListLabelMapper.selectByUserId(userId,loveId);
        List<LabelTypeVo> resultList = BeanCopyUtil.copyListFromSrcToDest(list,LabelTypeVo.class);
        return Response.success(resultList);
    }
}
